<?php

namespace app\admin\controller\questions;

use app\admin\{
    controller\Controllers,
    service\questions\Answer as AnswerService,
    model\questions\Answer as AnswerModels,
    service\questions\Question,
    service\questions\Opt
};
use think\Db;

class Answer extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( AnswerService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\questions\Answer.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'config' => config('field.answer')
        ]);
        return $this->fetch('questions/answer/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create( Question $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\questions\Answer.qid');
        $config = config('field.answer');
        $optConfig = config('field.opt');
        $this->assign([
            'config' => $config,
            'question' => $service->getList(),
            'optConfig' => $optConfig,
            'get' => $get
        ]);
        return $this->fetch('questions/answer/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(AnswerService $service, Opt $optService)
    {
        $post = $this->request->only(['title','qid','type', 'opttitle', 'is_answer','analysis'], 'post');
        $this->validate($post, 'app\admin\validate\questions\Answer.Create');
        $check = false;
        foreach ( $post['opttitle'] as $key => $_opttitle ) {
            if ( $post['is_answer'][$key] == 1 ) {
                $check = true;
                continue;
            }
        }
        if ( false === $check ) {
            return failed('必须有一个正确答案');
        }
        Db::startTrans();
        $answerid = $service->create($post);
        if ( false === $answerid || false === $optService->create($post, $answerid) ) {
            Db::rollback();
            return failed('新增失败');
        }
        Db::commit();
        return success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(AnswerModels $model, Question $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\questions\Answer.qid');
        $config = config('field.answer');
        $optConfig = config('field.opt');
        $this->assign([
            'config' => $config,
            'question' => $service->getList(),
            'data' => $model,
            'optConfig' => $optConfig,
            'get' => $get
        ]);
        return $this->fetch('questions/answer/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  AnswerService $service
     * @param  AnswerModels $model
     * @return \think\Response
     */
    public function update(AnswerService $service, AnswerModels $model, Opt $optService)
    {
        $put = $this->request->only(['title','type','qid','opttitle','is_answer','optId','analysis'], 'put');
        $this->validate($put, 'app\admin\validate\questions\Answer.Update');
        $check = false;
        foreach ( $put['opttitle'] as $key => $_opttitle ) {
            if ( $put['is_answer'][$key] == 1 ) {
                $check = true;
                continue;
            }
            if ( $put['optId'][$key] <= 0 ) {
                return failed('参数错误,刷新重试');
            }
        }
        if ( false === $check ) {
            return failed('必须有一个正确答案');
        }
        $update = $service->save($model, $put);
        if ( false === $update || false === $optService->save($put, $model->id) ) {
            return failed('编辑失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, AnswerService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\questions\Answer.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 批量删除
     * @param AnswerService $service
     * @return \think\Response
     */
    public function destory(AnswerService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\questions\Answer.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
